/************** init.html **************/
function query_invReturnRetail(page) {
	sys_loading();
	var parm = $('#queryForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('invReturnRetail-query');
	sys_record_parm('invReturnRetail',parm);
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#ResultList').html(result);
		}
	});
}


function shield_invReturnRetail(code,name){
	$.Zebra_Dialog('确定要屏蔽吗？',{
		'type': 'question',
		'title': '警告' ,
		'buttons':[
	       {caption:'屏蔽',callback:function() {
	    	   var url = create_url('invReturnRetail-shield');
	    	   aidExecute_invReturnInfo(code, url);
			}},
	       {caption:'取消',callback:function(){}
	       }]
		});
}

function unshield_invReturnRetail(code, name) {
	$.Zebra_Dialog('确定要解除屏蔽吗？',{
	'type': 'question',
	'title': '警告' ,
	'buttons':[
       {caption:'解除',callback:function() {
    	   var url = create_url('invReturnRetail-unshield');
    	   aidExecute_invReturnRetail(code, url);
		}},
       {caption:'取消',callback:function(){}
       }]
	});
}
function aidExecute_invReturnRetail(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				query_invReturnRetail();
			} else {
				sys_msg('操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}
//显示详情
function showSerial_invReturnRetail(code) {
	var url = create_url('invReturnRetail-detail');
	var parm = {'code':code};
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'800px', height:'800px',
		open:true,onClosed:function(){
		}
	});
}
//查询商品
function aidQueryCn_invReturnRetail(page) {
	sys_loading();
	var parm = $('#aidQueryCnForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('invReturnRetail-sList');
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
			sys_loading_close();
			$('#ResultList').html(result);
			}
		});
	}

function selectCn_invReturnRetail(btn) {
	var i = btn.attr('pib');
	var parm = {'index':i};
	var url = create_url('invReturnRetail-sInit');
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'900px', height:'700px', open:true, 
			onClosed:function(){
			}
		});
	}

//选择
function chooseCn_invReturnRetail(cc,cname,snc) {
	var p = $('#aid_g_p').val();
	$('#cc_' + p).val(cc);
	$('#cname_' + p).val(cname);
	$('#snc_' + p).val(snc);
	if (snc == 1 || snc == ''){
		$('#count_' + p).removeAttr('readonly','readonly');
		$('#biao_' + p).css('display','none');
		$('#count_' + p).val('');
		$('#s_' + p).html("无需序列号");
		$('#div_biao_' + p).empty();//清空
		/*$('#div_biao_ '+ p).html('');*/
	} else if(snc == 2){
		var url = create_url('invReturnRetail-check');
		var parm = {'snc' : snc,'cc' : cc};
		$.ajax({type:'POST', url:url, data:parm,
			success : function(serial) {
				if (serial == '' || serial == 'null' || undefined == serial) {
					$('#count_' + p).removeAttr('onMouseUp', '');
					$('#count_' + p).removeAttr('readonly','readonly');
					$('#s_' + p).html("同类序列号");
					$('#biao_' + p).show();
				} else {
					$('#count_' + p).removeAttr('onMouseUp', '');
					$('#count_' + p).removeAttr('readonly','readonly');
					$('#s_' + p).html("同类序列号,序列号为：" + serial);
					$('#biao_' + p).css('display','none');
				}
			}
		});
		
	} else if(snc == 3){
		$('#s_' + p).html("唯一序列号");
		$('#count_' + p).attr('readonly','readonly');
		$('#biao_' + p).show();
	}
		$.fn.colorbox.close();
}
//查询仓库
function aidQueryDn_invReturnRetail(page) {
	sys_loading();
	var parm = $('#aidQueryDnForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('invReturnRetail-aidList1');
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
			sys_loading_close();
			$('#aidResultList').html(result);
			}
		});
	}

function selectDn_invReturnRetail(btn) {
	var i = btn.attr('pib');
	var parm = {'index':i};
	var url = create_url('invReturnRetail-aidInit1');
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'900px', height:'700px', open:true, 
			onClosed:function(){
			}
		});
	}

//选择仓库
function chooseDn_invReturnRetail(code, name) {
	 var p = $('#aid_g_p').val();
	$('#dc_' + p).val(code);
	$('#dname_' + p).val(name);
	$.fn.colorbox.close();
}
//查询用户
function aidQueryUser_invReturnRetail(page) {
	sys_loading();
	var parm = $('#queryUserForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('invReturnRetail-aidList');
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
			sys_loading_close();
			$('#aidResultList').html(result);
			}
		});
	}

function selectUser_invReturnRetail(btn) {
	var parm = {'id':'smc', 'name':'sname'};
	var url = create_url('invReturnRetail-aidInit');
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'900px', height:'700px', open:true, 
			onClosed:function(){
			}
		});
	}

//选择销售员
function chooseUser_invReturnRetail(code, realName) {
	$('#smc').val(code);
	$('#sname').val(realName);
	$.fn.colorbox.close();
}
//显示序列号行
function show_invReturnRetail(i){
	$('#biao_' + i).show();
}

//删除多余的空白行
function delete_invReturnRetail(btn,i) {
	var p = btn.parents('.rep');
	if (!p.next().hasClass('rep')) {
		p.next().remove();
	}
	p.remove();
	num_invReturnRetail(i);
}
//添加空白行
function add_invReturnRetail() {
	var tempTbble = $('#tempTable');
	var tr = tempTbble.find('.rep').clone(true);
	var	tr1 = tempTbble.find('.rep').next().clone(true);
	var i = $('#index').val();
	tr.find('.cname').attr({'id':'cname_' + i, 'name':'itemList[' + i + '].cname'}).end()
		.find('.btn-primary').attr('pib', i).end()
		.find('.cc').attr({'id': 'cc_' + i, 'name': 'itemList[' + i + '].cc'}).end()
		.find('.snc').attr({'id':'snc_' + i, 'name':'itemList[' + i + '].snc'}).end()
		.find('.s').attr('id', 's_' + i).end()
		.find('.count').attr({'id' : 'count_' + i,'name': 'itemList[' + i + '].count'}).end()
		.find('.price').attr({'id' : 'p_' + i, 'name': 'itemList[' + i + '].price','value': '0'}).end()
		.find('.dname').attr('id', 'dname_' + i).end()
		.find('.btn-primary').attr('pib', i).end()
		.find('.dc').attr({'id': 'dc_' + i, 'name': 'itemList[' + i + '].dc'}).end();
	$('#ListBody').append(tr);
	tr1.find('td').first().attr({'id':'biao_' + i}).end()
		.find('.add-tag-input').attr({'i': i}).end()
		.find('.div').attr({'id':'div_biao_' + i}).end();
	$('#ListBody').append(tr1);

	$('#index').val(parseInt(i) + 1);
}

//检查数量输入只能是数字
function checkNum_invReturnRetail(obj) {
	var v = obj.val();
	if (/^[1-9][0-9]*$/.test(v)) {
	} else {
		sys_msg('只能填数字,而且不能以0开头哦！');
		obj.val('');
	}
	obj.css('border-color','');
}

//添加序列号
function addSerial_invReturnRetail(obj) {
	var v = obj.val().trim(); 
	if ('' == v || undefined == v) {
		sys_msg('没有输入任何信息');
		return;
	}
	 // 判断页面是否重复
	var ok = false;
		$('#ListBody').find('input.t').each(function(e) {
			var b = $(this).val(); 
			if (v === b) {
				sys_msg('序列号重复');
				obj.val('');
				ok = true;
				return;
			} 
		});
		if (ok) {
			return false;
		}
	// 判断后台
	var i = obj.attr('i');
	var s = $('#snc_' + i).val();
	var t = $('#div_biao_' + i).find('input.t').val();
	if (s == 2 && (t != null || undefined != t)){
		sys_msg('同类序列号只需输入一个序列号填入数量即可   如需修改请删除已有序列号！');
		obj.val('');
		return false;
	}
	var url = create_url('invReturnRetail-verify');
	var parm = {'serial' : v };
	$.ajax({type:'POST', url:url, data:parm,
		success : function(result) {
			if ('error' == result) {
				sys_msg('该序列号已入库');
				obj.val('');
			} else if ('success' == result) {
				
				var j = parseInt($('#biao_' + i).attr('j'));	// 获取变量j 
				var input = $('#temp').find('.rep').clone(true);
				input.find('.t').attr({'id': 't_' + j, 'name': 'itemList[' + i + '].detailList[' + j + '].serial',"value": obj.val()}).end()
				.find('.del-tag').attr({'onclick':'javascript: delete_invReturnRetail($(this),' + i + ');'}).end()
				.find('.t').next().html(obj.val()).end();
				
				$('#div_biao_' + i).append(input);
				$('#biao_' + i).css('display','');
				
				j++;
				$('#biao_' + i).attr("j", j);
				
				obj.val('');
				// 计算标签数量
				num_invReturnRetail(i);
			}
		}
	});
}
// 统计标签数量
function num_invReturnRetail(i) {
	var td = $('#biao_' + i); // 获取对应的TD
	var num = 0; // 计数器
	td.find('.t'); // 获取所有的input
	$.each(td.find('.t'), function() {
		var v = this.value;
		if ('' != v && undefined != v) {
			num++;
		}
	});
	$('#count_' + i).val(num);//给cou 赋值
	price_invReturnRetail(i);
} 
function price_invReturnRetail(){
	var total_ = 0;
	$.each($('#ListBody').find('tr.rep'), function(e) {
		var count = parseInt($(this).find('input.count').val());
		var price = parseFloat($(this).find('input.price').val());
		
		total_ = total_ + count*price;
	});
	$('#total1').val(total_);
	$('#total').val(total_);
}

// 保存零售信息
function save_invReturnRetail() {
	//验证输入框是否有未填写的
	var ok = false;
	$('#editForm').find('input.not-null').each(function(e) {
		if ('' == this.value || undefined == this.value) {
			$(this).css('border-color','red');
			ok = false;
		} else {
			ok = true;
		}
	});
	if (!ok) {
		$.Zebra_Dialog('不能有空值！');
		return;
	}
	var parm = $('#editForm').serialize();
	var url = create_url('invReturnRetail-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
			if (result == 'success') {
				$.Zebra_Dialog('操作成功！');
				sys_load_handle('invReturnRetail');
			}else if (result == 'error') {
				$.Zebra_Dialog('操作失败！');
			} else if(result == 'exists'){
				$.Zebra_Dialog('对不起！该物品库存为0!');
			} else if(result == 'not_find'){
				$.Zebra_Dialog('对不起！库存不足!');
			}
		}
	});
}


